package com.fashop.util;

import java.io.BufferedInputStream;
import org.apache.commons.codec.binary.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilterOutputStream;
import java.io.IOException;

import org.springframework.stereotype.Service;

@Service("utilidades")
public class Utilidades {
	private final String RUTA_IMAGENES_SERVER="/home/fashop/";
	private final String RUTA_IMAGENES_LOCAL="D:/Proyectos/Fantasma/SW-Fant/imagenes/";
	public final String convertImagenBytes(String img){
		byte[] image 		= null;
		byte[] imageBase64	= null;
		String sImagenB64	= null;
		//System.out.println("Ruta: "+ System.getProperty("user.dir"));
		FileInputStream in;
		FilterOutputStream imfilter;
		File file;
		try {
			
			file = 	new File (RUTA_IMAGENES_SERVER+img);
			in = new FileInputStream(file);		
			BufferedInputStream reader = new BufferedInputStream(in);
			int length = reader.available(); 
			image = new byte[length]; 
			reader.read(image, 0, length);
			imageBase64=Base64.encodeBase64(image);
			sImagenB64=new String(imageBase64);
			System.out.println("imagen codificada:"+sImagenB64);
			reader.close();
			
		
			
		} 
		catch (FileNotFoundException e) {e.printStackTrace();}
		catch (IOException e) {e.printStackTrace();}
		catch (NullPointerException e){e.printStackTrace();}
		//System.out.println(image.toString());
		return sImagenB64;		
	}
	

}
